package net.ecpt.hrm.services;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ServiceUtil;
import org.ofbiz.wvtools.utils.ExcelPoiUtils;

import javolution.util.FastList;
import javolution.util.FastMap;

public class HrObjectiveServices {
    public static final String module = HrObjectiveServices.class.getName();


    /**
     * 导入
     */
    public static Map<String, Object> importXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Map<String, Object> result = ServiceUtil.returnSuccess();
	Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
	Delegator delegator = dctx.getDelegator();
	List<GenericValue> toBeStore = new ArrayList();
	List<String> messageList = new ArrayList<String>();
	List<Map<String, Object>> sheetList = (List<Map<String, Object>>) context.get("sheetList");
	LocalDispatcher dispatcher = dctx.getDispatcher();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	String createBy = userLogin.getString("partyId");

	try {
	    int i = 0;
	   
	    for (Map<String, Object> row : sheetList) {
		i = i + 1;
		if(i>1) {
			String id = ExcelPoiUtils.getCellString(row, 0);
			String typeId_dictText = ExcelPoiUtils.getCellString(row, 1);
			String partyId_dicText = ExcelPoiUtils.getCellString(row, 2);
			String objectiveName = ExcelPoiUtils.getCellString(row, 3);
			String completionTime = ExcelPoiUtils.getCellString(row, 4);
			String createDate = ExcelPoiUtils.getCellString(row, 5);
			//"目标类型，负责人，目标名称，预计完成时间，创建时间
			
			// 判断
			if (UtilValidate.isEmpty(typeId_dictText)) {
			    messageList.add("第" + (i + 1) + "行,目标类型不能为空。");
			    continue;
			}

			// 判断
			if (UtilValidate.isEmpty(partyId_dicText)) {
			    messageList.add("第" + (i + 1) + "行,负责人不能为空。");
			    continue;
			}
			// 判断
			if (UtilValidate.isEmpty(objectiveName)) {
			    messageList.add("第" + (i + 1) + "行,目标名称不能为空。");
			    continue;
			}
			
			// 判断
			if (UtilValidate.isEmpty(completionTime)) {
			    messageList.add("第" + (i + 1) + "行,预计完成时间不能为空。");
			    continue;
			}

			// 判断
			if (UtilValidate.isEmpty(createDate)) {
			    messageList.add("第" + (i + 1) + "行,创建时间不能为空。");
			    continue;
			}

			
			// 创建
			GenericValue entity = null;
			if (UtilValidate.isNotEmpty(id)) {
			   
			    List<GenericValue> entityList = delegator.findByAnd("HrObjective",
					    UtilMisc.toMap("id", id), null, false);
				    entity = EntityUtil.getFirst(entityList);
				if (UtilValidate.isEmpty(entity)) {
						 id = delegator.getNextSeqId("HrObjective");
						 entity = delegator.makeValue("HrObjective", UtilMisc.toMap("id", id));
						 entity.set("statusId", "0");
							entity.set("createBy", createBy);
							entity.set("createDate", nowTimestamp);
						} 	   
			}else  {
			    id = delegator.getNextSeqId("HrObjective");
			    entity = delegator.makeValue("HrObjective", UtilMisc.toMap("id", id));
			    entity.set("statusId", "0");
				entity.set("createBy", createBy);
				entity.set("createDate", nowTimestamp);
			}  
			
			
			String typeId = ExcelPoiUtils.getDataDictionaryCode(delegator, "OBJECT_TYPE",typeId_dictText);
			
			List<GenericValue> personInfo = delegator.findByAnd("Person", UtilMisc.toMap("name", partyId_dicText),null, false);
			GenericValue person =EntityUtil.getFirst(personInfo);
			String partyId = person.getString("partyId");
			
			
			Timestamp createDateTimestamp =UtilDateTime.toTimestamp(createDate);
			
			
			entity.set("typeId", typeId);
			entity.set("partyId", partyId);
			entity.set("objectiveName", objectiveName);
			entity.set("completionTime", completionTime);
			entity.set("createDate", createDateTimestamp);
			
			
			toBeStore.add(entity);
			

			
		}
	    }
	    delegator.storeAll(toBeStore);
	    result.put("messageList", messageList);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    /**
     * 导出
     */

    public static Map<String, Object> exportXls(DispatchContext dctx, Map<String, ? extends Object> context) {
	Delegator delegator = dctx.getDelegator();
	String ids = (String) context.get("ids");
	List<String> baseIdList = FastList.newInstance();
	if (UtilValidate.isNotEmpty(ids)) {
	    baseIdList = Arrays.asList(ids.split(";"));
	}
	Map<String, Object> result = ServiceUtil.returnSuccess();

	EntityCondition mainCond = null;
	List<EntityCondition> andExprs = FastList.newInstance();
	//andExprs.add(EntityCondition.makeCondition("parentProductId", EntityOperator.EQUALS, "_NA_"));
	if (UtilValidate.isNotEmpty(baseIdList))
	    andExprs.add(EntityCondition.makeCondition("id", EntityOperator.IN, baseIdList));
	if (andExprs.size() > 0)
	    mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);

	try {
	    List<GenericValue> entityList = delegator.findList("HrObjective", mainCond, null, null, null, true);

	    List<Map<String, Object>> list = FastList.newInstance();
	    Map<String, Object> keyMap = FastMap.newInstance();
	    keyMap.put("0", "id");
	    keyMap.put("1", "typeId_dictText");
	    keyMap.put("2", "partyId_dicText");
	    keyMap.put("3", "objectiveName");
	    keyMap.put("4", "completionTime");
	    keyMap.put("5", "createDate");
	    String headers = "编号,目标类型,负责人,目标名称,预计完成时间,创建时间";
	    result.put("headers", headers);
	    
	    for (GenericValue entity : entityList) {
		Map<String, Object> map = FastMap.newInstance();
		String id = entity.getString("id");
		String typeId = entity.getString("typeId");
		String partyId = entity.getString("partyId");
		String objectiveName = entity.getString("objectiveName");
		String completionTime = entity.getString("completionTime");
		Timestamp createDate = entity.getTimestamp("createDate");
		
		String typeId_dicText = ExcelPoiUtils.getDataDictionaryDescription(delegator, "OBJECT_TYPE",typeId);
		
		GenericValue person = delegator.findOne("Person", UtilMisc.toMap("partyId", partyId), false);
	    String partyId_dicText = person.getString("name");
	    
		map.put("id", id);
		map.put("typeId_dictText", typeId_dicText);
		map.put("partyId_dicText", partyId_dicText);
		map.put("objectiveName", objectiveName);
		map.put("completionTime", completionTime);
		map.put("createDate", createDate);

		list.add(map);
	    }
	    result.put("list", list);
	    result.put("keyMap", keyMap);
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	}
	return result;
    }

    
}
